package ${basePackage}.cli.command;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.json.JSONUtil;
import ${basePackage}.generator.MainGenerator;
import ${basePackage}.model.DataModel;
import lombok.Data;
import picocli.CommandLine;

import java.util.concurrent.Callable;

/**
 * json-generator子命令逻辑
 */
@Data
@CommandLine.Command(name = "json-generate", description = "读取json文件生成代码", mixinStandardHelpOptions = true)
public class JsonGeneratorCommand implements Callable<Integer> {

    @CommandLine.Option(names = {"--f","--file"}, description = "json文件路径", arity = "0..1", interactive = true, echo = true)
    private String filePath;

    @Override
    public Integer call() throws Exception {
        // 使用hutool工具类读取josn文件
        String jsonStr = FileUtil.readUtf8String(filePath);
        // 利用hutool工具包将json转成DataModel对象
        DataModel dataModel = JSONUtil.toBean(jsonStr, DataModel.class);
        // 执行核心生成器中的doGenerator方法
        MainGenerator.doGenerate(dataModel);
        return 0;
    }
}